package cn.t.repository.impl.user;

import cn.t.converter.user.CustomerFirmConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.CustomerFirmDO;
import cn.t.persistence.entity.user.CustomerFirmPO;
import cn.t.persistence.mapper.user.CustomerFirmMapper;
import cn.t.repository.user.CustomerFirmRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 单位信息表数据仓库
 *
 * @author l
 */
@Slf4j
@Repository
@AllArgsConstructor
public class CustomerFirmRepositoryImpl extends RepositoryImpl<CustomerFirmMapper, CustomerFirmPO, CustomerFirmConverter, CustomerFirmDO> implements CustomerFirmRepository {


    @Override
    public List<CustomerFirmDO> findByModel(CustomerFirmDO customerFirmDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<CustomerFirmPO> list = getWrapper(customerFirmDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<CustomerFirmPO> getWrapper(CustomerFirmDO customerFirmDO) {
        LambdaQueryChainWrapper<CustomerFirmPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(customerFirmDO.getId())) {
            wrapper.eq(CustomerFirmPO::getId, customerFirmDO.getId());
        }
        if (!ObjectUtils.isEmpty(customerFirmDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(customerFirmDO.getGmtCreatedEnd())) {
            wrapper.between(CustomerFirmPO::getGmtCreated, customerFirmDO.getGmtCreatedStart(), customerFirmDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(customerFirmDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(customerFirmDO.getGmtModifiedEnd())) {
            wrapper.between(CustomerFirmPO::getGmtModified, customerFirmDO.getGmtModifiedStart(), customerFirmDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(customerFirmDO.getParentId())) {
            wrapper.eq(CustomerFirmPO::getParentId, customerFirmDO.getParentId());
        }
        if (!ObjectUtils.isEmpty(customerFirmDO.getCustomerId())) {
            wrapper.eq(CustomerFirmPO::getCustomerId, customerFirmDO.getCustomerId());
        }
        if (!ObjectUtils.isEmpty(customerFirmDO.getFirmName())) {
            wrapper.like(CustomerFirmPO::getFirmName, customerFirmDO.getFirmName() + "%");
        }
        if (!ObjectUtils.isEmpty(customerFirmDO.getFirmCode())) {
            wrapper.like(CustomerFirmPO::getFirmCode, customerFirmDO.getFirmCode() + "%");
        }


        if (!ObjectUtils.isEmpty(customerFirmDO.getIsDeleted())) {
            wrapper.eq(CustomerFirmPO::getIsDeleted, customerFirmDO.getIsDeleted());
        } else {
            wrapper.eq(CustomerFirmPO::getIsDeleted, YesNoEnum.FALSE.getCode());
        }

        if (!ObjectUtils.isEmpty(customerFirmDO.getStatus())) {
            wrapper.eq(CustomerFirmPO::getStatus, customerFirmDO.getStatus());
        } else {
            wrapper.eq(CustomerFirmPO::getStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        }
        wrapper.in(!CollectionUtils.isEmpty(customerFirmDO.getFirmIds()), CustomerFirmPO::getId, customerFirmDO.getFirmIds());
        return wrapper;
    }

    @Override
    public long count(CustomerFirmDO customerFirmDO) {
        return getWrapper(customerFirmDO).count();
    }
}
